package com.zxy.ziems.server.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zxy.btp.common.exception.ConditionException;
import com.zxy.ziems.server.domain.Resource;
import com.zxy.ziems.server.domain.RoleResourceRel;
import com.zxy.ziems.server.dto.ResourceTree;
import com.zxy.ziems.server.mapper.RoleResourceRelMapper;
import com.zxy.ziems.server.service.ResourceService;
import com.zxy.ziems.server.service.RoleResourceRelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description
 * @ClassName RoleResourceRelServiceImpl
 * @Author 邓伟
 * @date 2022.04.18 17:02
 */
@Service
public class RoleResourceRelServiceImpl extends ServiceImpl<RoleResourceRelMapper,RoleResourceRel> implements RoleResourceRelService {

    @Autowired
    private RoleResourceRelMapper roleResourceRelMapper;
    @Autowired
    private ResourceService resourceService;


    @Override
    public List<ResourceTree> listByRoleId(String roleId) {
        List<String> roleIdList = new ArrayList<>();
        roleIdList.add(roleId);
        return listByRoleId(roleIdList);
    }

    @Override
    public List<ResourceTree> listByRoleId(List<String> roleIdList) {
        QueryWrapper<RoleResourceRel> query = new QueryWrapper<>();
        query.lambda().in(RoleResourceRel::getRoleId,roleIdList);
        List<RoleResourceRel> roleRel = roleResourceRelMapper.selectList(query);
        if (ObjectUtil.isEmpty(roleRel) || roleRel.isEmpty()) {
            throw new ConditionException("未查询到角色关联资源数据");
        }
        //根据关联关系查询资源信息
        List<String> resourceIdList = roleRel.stream().distinct().map(RoleResourceRel::getResourceId).collect(Collectors.toList());
        //根据资源id查询资源树
        List<ResourceTree> resourceTreeList = resourceService.queryResourceTreeByResourceIdList(resourceIdList);
        return resourceTreeList;
    }

    @Override
    public Integer deleteByCondition(QueryWrapper<RoleResourceRel> query) {
        return roleResourceRelMapper.delete(query);
    }

    @Override
    public List<ResourceTree> getAllResourceTree() {
        return resourceService.queryResourceTreeByResourceIdList(null);
    }

    @Override
    public List<Resource> listByRoleIds(List<String> roleIdList) {
        QueryWrapper<RoleResourceRel> query = new QueryWrapper<>();
        query.lambda().in(RoleResourceRel::getRoleId,roleIdList);
        List<RoleResourceRel> roleRel = roleResourceRelMapper.selectList(query);
        if (ObjectUtil.isEmpty(roleRel) || roleRel.isEmpty()) {
            throw new ConditionException("未查询到角色关联资源数据");
        }
        //根据关联关系查询资源信息
        List<String> resourceIdList = roleRel.stream().distinct().map(RoleResourceRel::getResourceId).collect(Collectors.toList());
        return resourceService.listByIds(resourceIdList);
    }

    @Override
    public List<RoleResourceRel> roleResourceRelListByRoleIds(List<String> roleIdList) {
        QueryWrapper<RoleResourceRel> query = new QueryWrapper<>();
        query.lambda().in(RoleResourceRel::getRoleId,roleIdList);
        List<RoleResourceRel> roleRel = roleResourceRelMapper.selectList(query);
        return roleRel;
    }

    @Override
    public List<RoleResourceRel> roleResourceRelListByRoleId(String roleId) {

        QueryWrapper<RoleResourceRel> query = new QueryWrapper<>();
        query.lambda().eq(RoleResourceRel::getRoleId,roleId);
        List<RoleResourceRel> roleRel = roleResourceRelMapper.selectList(query);
        return roleRel;
    }
}
